package com.zyj.exam_app.login.service.impl;

import com.zyj.exam_app.context.UserContext;
import com.zyj.exam_app.login.bean.LoginInfo;
import com.zyj.exam_app.login.service.LoginService;
import com.zyj.exam_app.menu_manager.bean.MenuBean;
import com.zyj.exam_app.menu_manager.bean.MenuFactory;
import com.zyj.exam_app.user_manager.bean.UserBean;
import com.zyj.exam_app.user_manager.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class LoginServiceImpl implements LoginService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginInfo doLogin(UserBean userInfo){
        UserBean userBean = userMapper.findUserByAccountAndPassword(userInfo);
        if(userBean == null){
            return null;
        }
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUserInfo(userBean);
        //获取用户菜单
        List<MenuBean> menus = MenuFactory.createTestMenus(userBean);
        loginInfo.setUserMenus(menus);
        //缓存用户信息
        UserContext.putCurrebtUser(loginInfo);
        return loginInfo;
    }

    @Override
    public boolean checkUserHasLogin(){
        return null == UserContext.getCurreentUser();
    }


}
